package com.ctshk.rpc.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.goods.dto.CommodityLogDTO;
import com.ctshk.rpc.goods.entity.CommodityLog;
import com.ctshk.rpc.goods.mapper.CommodityLogMapper;
import com.ctshk.rpc.goods.req.CommodityLogPageReq;
import com.ctshk.rpc.goods.service.ICommodityLogService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 商品日志表 服务实现类
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-16
 */
@DubboService
public class CommodityLogServiceImpl extends ServiceImpl<CommodityLogMapper, CommodityLog> implements ICommodityLogService {

    @Autowired
    private CommodityLogMapper commodityLogMapper; // 商品日志mapper

    @Override
    public PageResponse<CommodityLogDTO> findCommodityLogsByCommodityId(CommodityLogPageReq pageReq, TokenUser tokenUser) {
        if (null == pageReq || null == pageReq.getCommodityId()) {
            throw new BusinessException(SystemError.SYS_402);
        }
        if (tokenUser == null) {
            throw new BusinessException(SystemError.SYS_406);
        }
        IPage<CommodityLogDTO> page = new Page<CommodityLogDTO>(pageReq.getPageNo(),pageReq.getPageSize());
        IPage<CommodityLogDTO> iPage = commodityLogMapper.findCommodityLogPageByCondition(page, pageReq);
        List<CommodityLogDTO> recordsList = iPage.getRecords();
        List<CommodityLogDTO> pageDTOList = EntityUtil.copyList(recordsList, CommodityLogDTO.class);
        return new PageResponse<>(pageDTOList, iPage.getCurrent() == 1,
                iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()),
                iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }
}
